<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\controller\Payment;
use app\common\model\FishingGroundTypes;
use think\Controller;
use think\Db;
use think\Request;


class Fishingground extends Api
{
    private static $model;
    protected $noNeedLogin = ['lists'];
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new \app\common\model\FishingGround();
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function lists()
    {
        $lon = input('longitude');
        $lat = input('latitude');
        if (!$lon || !$lat) {
            $this->error('请提供完整的定位！');
        }

        $sql = "SELECT  g.*,(st_distance (point (lon, lat),point({$lon},{$lat}) ) / 0.0111) AS distance  FROM  fa_fishing_ground g where g.status = 1";
        $order = "distance asc";

        $source_type = input('source_type');
        $pageSize = input('pagesize', 15);
        $page = input('page', 1);//页码
        $start = ($page - 1) * $pageSize;

        if (!$source_type) {
            // 不是我上传的钓场
            $distance = input('distance');//距离
            $district = input('district');//区域

            $order_type = input('order_type');//排序

            $ground_type = input('ground_type');//钓场类型
            $fee_type = input('fee_type');//收费类型

            if ($order_type) {
                $order = "g.{$order_type} desc";
            }

            if ($distance) {
                $sql .= " and g.distance <= $distance";
            }
            if ($district) {
                $sql .= " and g.distrct = $district";
            }
            if ($ground_type) {
                $sql .= " and g.ground_type = $ground_type";
            }
            if ($fee_type) {
                $sql .= " and g.fee_type = $fee_type";
            }

            $is_recom = input('is_recom');
            if ($is_recom) {
                $sql .= " and g.is_recom = 1";
            }

            $all = Db::query($sql);
        } else {
            if (!in_array($source_type, [1, 2])) {
                $this->error('参数非法！');
            }

            $sql .= " and g.source_type = $source_type";
            $all = Db::query($sql);
        }
        $total = count($all);
        $sql .= " ORDER BY {$order} limit {$start},{$pageSize}";
        $list = Db::query($sql);
        if ($list) {
            $fishing_ground_types = config('site.fishing_ground_types');
            foreach ($list as &$val) {
                $val['image'] = get_full_img($val['image']);
                if ($val['pics']) {
                    $picsArr = explode(',', $val['pics']);
                    $val['pics'] = array_map('get_full_img', $picsArr);
                }

                $ground_type_names = [];
                if ($val['source_type'] == 1) {
                    $ground_type_names = [$fishing_ground_types[$val['ground_type']]];
                } elseif ($val['source_type'] == 2) {
                    $type_names = FishingGroundTypes::where('ground_id', $val['id'])->group('type')->column('type');
                    if (count($type_names) > 0) {
                        foreach ($type_names as $vv) {
                            $ground_type_names[] = $fishing_ground_types[$vv];
                        }
                    }
                }

                $val['ground_type_names'] = $ground_type_names;

                if ($val['distance'] >= 1000) {
                    $val['distance'] = round($val['distance'] * 0.001, 1) . 'km';
                } else {
                    $val['distance'] = floor($val['distance']) . 'm';
                }

                $val['star'] = round($val['star'], 1);
            }
        }

        $this->success('钓场列表',[
            'total' => $total,
            'per_page' => $pageSize,
            'current_page' => $page,
            'data' => $list,
        ]);
    }

    /**
     * 用户上传野钓场
     */
    public function create()
    {
        $name = $this->request->post('name');
        $province = $this->request->post('province');
        $city = $this->request->post('city');
        $district = $this->request->post('district');
        $address = $this->request->post('address');
        $lon = $this->request->post('lon');
        $lat = $this->request->post('lat');
        $fish_type = $this->request->post('fish_type');
        $identity = $this->request->post('identity');
        $mobile = $this->request->post('mobile');
        $pics = $this->request->post('pics');
        $intro = $this->request->post('intro');
        $rules = $this->request->post('rules');

        $data = [
            'name' => $name,
            'province' => $province,
            'city' => $city,
            'district' => $district,
            'address' => $address,
            'lon' => $lon,
            'lat' => $lat,
            'fish_type' => $fish_type,
            'identity' => $identity,
            'mobile' => $mobile,
            'pics' => $pics,
            'intro' => $intro,
            'rules' => $rules,
        ];

        $rule = [
            'name' => 'require',
            'district' => 'require',
            'address' => 'require',
            'lon' => 'require',
            'lat' => 'require',
            'fish_type' => 'require|number',
//            'mobile' => 'require|length:11',
            'pics' => 'require',
        ];
        $msg = [
            'name.require' => '请输入钓场名称',
            'district.require' => '省市区必选',
            'address.require' => '请输入详细地址',
            'lon.require' => '经度信息缺失',
            'lat.require' => '纬度信息缺失',
            'fish_type.require' => '必须选择鱼种',
            'fish_type.number' => '鱼种字段类型错误',
//            'mobile.require' => '请输入电话号码',
//            'mobile.length' => '电话号码长度必须是11位',
            'pics.require' => '必须上传钓场图片',
        ];

        $validate = new \think\Validate($rule, $msg);
        $result = $validate->check($data);
        if (!$result) {
            $this->error($validate->getError());
        }

        $picsArr = explode(',', $pics);
        if (count($picsArr) < 6 || count($picsArr) > 15) {
            $this->error('钓场图片必须在6-15张之间！');
        }

        $data['image'] = $picsArr[0];

        $ret = \app\common\model\FishingGround::create($data);
        if (!$ret) {
            $this->error('添加失败！');
        }

        $this->success('添加成功！');
    }

    /**
     * 钓场详情
     */
    public function read()
    {
        $id = input('id');
        if (!$id) {
            $this->error('参数缺失！');
        }

        $groundInfo = self::$model->where('status', 1)->find();
        if (!$groundInfo) {
            $this->error('该钓场不存在！');
        }

        $fishing_ground_types = config('site.fishing_ground_types');
        $fish_types = config('site.fish_types');
        $typesArr = $groundInfo->types;

        if ($typesArr) {
            foreach ($typesArr as $v1) {
                $fishs = $v1->fishs;
                if ($fishs) {
                    foreach ($fishs as $v3) {
                        $v3->type_name = $fish_types[$v3->type];
                        $v3->positions;
                    }
                }

                $v1->type_name = $fishing_ground_types[$v1->type];
            }
        }

        $prices = $groundInfo->prices;
        if ($prices) {
            foreach ($prices as $v2) {
                $v2->type_name = $fishing_ground_types[$v2->type];
            }
        }

        $this->success('钓场详情', $groundInfo);
    }
}
